package com.example.week9_advanceui;

import android.content.Context;
import android.content.Intent;
import android.support.v7.view.menu.MenuView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;



public class TextImageAdapter extends BaseAdapter{
    private Context mContext;

    //展示的文字
    private List<String> texts;
    //展示的图片
    private List<Integer> images;

    public TextImageAdapter(Context mContext, List<String>texts, List<Integer> images)
    {
        this.mContext=mContext;
        this.texts=texts;
        this.images=images;
    }

    //元素的个数
    public int getCount()
    {
        return texts.size();
    }

    //取得选中的项目的值，本例中不支持

    public Object getItem(int i)
    {
        return null;
    }

    public long getItemId(int i)
    {
        return 0;
    }

      //用于生成在ListView中展示的一个View元素
    public View getView(int position, View convertView, ViewGroup parent)
    {
        //优化ListView
        if(convertView==null)
        {
            convertView= LayoutInflater.from(mContext).inflate(android.R.layout.activity_list_item ,null);
            ItemViewCache viewCache =new ItemViewCache();
            viewCache.mTextView=(TextView)convertView.findViewById(R.id.itemListView4Txt);
            viewCache.mImageView=(ImageView)convertView.findViewById(R.id.itemListView4Img);convertView.setTag(viewCache);
        }
        ItemViewCache cache=(ItemViewCache)convertView.getTag();
        //设置文本和图片，然后返回这个View，用于ListView的Item的展示
        cache.mTextView.setText(texts.get(position));
        cache.mImageView.setImageResource(images.get(position));
        return convertView;
    }

 private class ItemViewCache
 {
     public TextView mTextView;
     public ImageView mImageView;
 }

}
